package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.InpstockMapper;
import com.pojo.Inpstock;
import com.pojo.InpstockExample;
import com.pojo.JdWarehouse;
import com.pojo.Product;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InpstockService {
    
    @Resource
    private InpstockMapper inpstockMapper;
    @Resource
    private ProductService productService;
    @Resource
    private WarehouseService warehouseService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<Inpstock> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo){
    
        JdWarehouse warehouse = null;
        if (vo.getW_name() != null && !"".equals(vo.getW_name().trim())){
            List<JdWarehouse> warehouses = warehouseService.queryByType(new JdWarehouse(vo.getW_name().trim()));
            if (!warehouses.isEmpty()){
                warehouse = warehouses.get(0);
            }else {
                warehouse = new JdWarehouse(-1);
            }
        }
        Product product = null;
        if (vo.getP_id() != null && !"".equals(vo.getP_id().trim())) {
            List<Product> products = productService.queryByType(new Product(vo.getP_id().trim()));
            if (!products.isEmpty()){
                product = products.get(0);
            }else{
                product = new Product("null");
            }
        }
        
        InpstockExample example = new InpstockExample();
        InpstockExample.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (warehouse != null){
                criteria.andW_idEqualTo(warehouse.getW_id());
            }
            if (product != null) {
                criteria.andP_idEqualTo(product.getP_id().trim());
            }
            if (vo.getBeginDate()!=null){
                criteria.andIn_timeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate()!=null){
                criteria.andIn_timeLessThan(vo.getEndDate());
            }
        }
        
        //sql执行问题
        PageHelper.startPage(pageNum,pageSize);
        List<Inpstock> inpstocks = inpstockMapper.selectByExample(example);
        if (inpstocks.isEmpty()) {
            return new PageInfo<>(inpstocks);
        }
        //填充数据
        for (Inpstock inpstock : inpstocks) {
            
            List<Product> products = productService.queryByType(new Product(inpstock.getP_id()));
            Product productTmp = null;
            if (!products.isEmpty()){
                productTmp = products.get(0);
            }
            
            List<JdWarehouse> warehouses = warehouseService.queryByType(new JdWarehouse(inpstock.getW_id()));
            JdWarehouse warehouseTmp = null;
            if (!warehouses.isEmpty()){
                warehouseTmp = warehouses.get(0);
            }
            if (warehouseTmp != null){
                inpstock.setW_name(warehouseTmp.getW_name());
            }
            if (productTmp != null){
                inpstock.setP_name(productTmp.getP_name());
            }
        }
        return new PageInfo<>(inpstocks);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Inpstock> queryByAll(){
        return inpstockMapper.selectByExample(new InpstockExample());
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Inpstock> queryByDay(QueryVo vo){
        InpstockExample example = new InpstockExample();
        InpstockExample.Criteria criteria = example.createCriteria();
    
        if (vo != null) {
            if (vo.getBeginDate() != null) {
                criteria.andIn_timeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate() != null) {
                criteria.andIn_timeLessThanOrEqualTo(vo.getEndDate());
            }
        }
        return inpstockMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int delectInpstock(Integer in_num){
        return inpstockMapper.deleteByPrimaryKey(in_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int addInpstock(Inpstock inpstock){
        if (inpstock == null){
            return 0;
        }
        return inpstockMapper.insertSelective(inpstock);
    }
    
    @Transactional(propagation = Propagation.REQUIRED , rollbackFor = Exception.class)
    public Inpstock queryByKey(Integer in_num){
        return inpstockMapper.selectByPrimaryKey(in_num);
    }
    
    @Transactional(propagation = Propagation.REQUIRED , rollbackFor = Exception.class)
    public int update(Inpstock inpstock) {
        if(inpstock != null){
            return inpstockMapper.updateByPrimaryKeySelective(inpstock);
        }
        return 0;
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public int count(){
        InpstockExample example = new InpstockExample();
        InpstockExample.Criteria criteria = example.createCriteria();
        return (int) inpstockMapper.countByExample(example);
    }
    
    
}
